﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DIPLib.UI
{
    public partial class InputMatrixDialog : Form
    {
        private byte type;
        private int mMatrixSize = 3;
        private float[] mDataFloat = new float[9];
        private byte[] mDataByte = new byte[9];
        private int[] mDataInt = new int[9];

        public int MatrixSize
        {
            set
            {
                mMatrixSize = value;
                ResizeOurself(mMatrixSize, mMatrixSize);
                //
            }
            get
            {
                return mMatrixSize;
            }
        }

        public float[] getDataFloat()
        {
            ExtractTextboxes();
            return mDataFloat;
        }

        public byte[] getDataByte()
        {
            ExtractTextboxes();
            return mDataByte;
        }

        public int[] getDataInt()
        {
            ExtractTextboxes();
            return mDataInt;
        }

        public InputMatrixDialog(byte type)
        {
            this.type = type;
            InitializeComponent();
            ResizeOurself(mMatrixSize, mMatrixSize);
        }

        private void dgview_matrix_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            //this.dgview_matrix.EditingControl.KeyPress -= new KeyPressEventHandler(MatrixIO_KeyPress);
            //this.dgview_matrix.EditingControl.KeyDown -= new KeyEventHandler(this.MatrixIO_KeyDown);
            //this.dgview_matrix.EditingControl.KeyPress += new KeyPressEventHandler(MatrixIO_KeyPress);
            //this.dgview_matrix.EditingControl.KeyDown += new KeyEventHandler(this.MatrixIO_KeyDown);
        }

        private void AddARow(int i)
        {
            DataGridViewRow Arow = new DataGridViewRow();
            Arow.HeaderCell.Value = "R" + i.ToString();
            this.dgview_matrix.Rows.Add(Arow);
        }
        private void AddAColumn(int i)
        {
            DataGridViewTextBoxColumn Acolumn = new DataGridViewTextBoxColumn();
            Acolumn.HeaderText = "C" + i.ToString();
            Acolumn.Name = "Column" + i.ToString();
            Acolumn.Width = 60;
            Acolumn.SortMode = DataGridViewColumnSortMode.NotSortable;
            //make a Style template to be used in the grid
            DataGridViewCell Acell = new DataGridViewTextBoxCell();
            Acell.Style.BackColor = Color.LightCyan;
            Acell.Style.SelectionBackColor = Color.FromArgb(128, 255, 255);
            Acolumn.CellTemplate = Acell;
            this.dgview_matrix.Columns.Add(Acolumn);
        }
        private void ResizeOurself(int r, int c)
        {
            //adjust rows and cols, do nothing if they equal
            //

            while (c < this.dgview_matrix.ColumnCount)
            {
                this.dgview_matrix.Columns.RemoveAt(this.dgview_matrix.ColumnCount - 1);
            }
            while (c > this.dgview_matrix.ColumnCount)
            {
                AddAColumn(this.dgview_matrix.ColumnCount);
            }
            while (r < this.dgview_matrix.ColumnCount)
            {
                this.dgview_matrix.Rows.RemoveAt(this.dgview_matrix.RowCount - 1);
            }
            while (r > this.dgview_matrix.RowCount)
            {
                AddARow(this.dgview_matrix.RowCount);
            }
        }
        private void FillTextboxes() //fill the textboxes
        {
            int count = 0;
            for (int r = 0; r < this.dgview_matrix.RowCount; r++)
            {
                for (int c = 0; c < this.dgview_matrix.ColumnCount; c++)
                {
                    this.dgview_matrix[c, r].Value = mDataFloat[count++]; //notice r, c
                }
            }
        }

        private void ExtractTextboxes()
        {
            int count = 0;
            if (type == FilterUtils.FLOAT)
            {
                for (int r = 0; r < this.dgview_matrix.RowCount; r++)
                {
                    for (int c = 0; c < this.dgview_matrix.ColumnCount; c++)
                    {
                        try
                        {
                            string str = this.dgview_matrix[c, r].Value.ToString();
                            if (str.Contains("/"))
                            {
                                int k = str.IndexOf("/");
                                String left = str.Substring(0, k);
                                String right = str.Substring(k + 1);
                                float f = float.Parse(left) / float.Parse(right);
                                mDataFloat[count++] = f;
                            }
                            else
                            {
                                mDataFloat[count++] = float.Parse(str);
                            }

                        }
                        catch (Exception)
                        {
                            mDataFloat[count++] = 0f;
                        }
                    }
                }
            }
            else if (type == FilterUtils.BYTE)
            {
                for (int r = 0; r < this.dgview_matrix.RowCount; r++)
                {
                    for (int c = 0; c < this.dgview_matrix.ColumnCount; c++)
                    {
                        try
                        {
                            string str = this.dgview_matrix[c, r].Value.ToString();
                            {
                                byte b = byte.Parse(str);
                                if (b > 0)
                                {
                                    mDataByte[count++] = b;
                                }
                            }

                        }
                        catch (Exception)
                        {
                            mDataByte[count++] = 0;
                        }
                    }
                }
            }
            else if (type == FilterUtils.INT)
            {
                for (int r = 0; r < this.dgview_matrix.RowCount; r++)
                {
                    for (int c = 0; c < this.dgview_matrix.ColumnCount; c++)
                    {
                        try
                        {
                            string str = this.dgview_matrix[c, r].Value.ToString();
                            {
                                mDataInt[count++] = int.Parse(str);
                            }

                        }
                        catch (Exception)
                        {
                            mDataInt[count++] = 0;
                        }
                    }
                }
            }
        }

        private void cboxElementSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboxElementSize.SelectedIndex == 0)
            {
                this.MatrixSize = 3;
            }
            else if (cboxElementSize.SelectedIndex == 1)
            {
                this.MatrixSize = 5;
            }
            else if (cboxElementSize.SelectedIndex == 2)
            {
                this.MatrixSize = 9;
            }
            else if (cboxElementSize.SelectedIndex == 3)
            {
                this.MatrixSize = 15;
            }
            else if (cboxElementSize.SelectedIndex == 4)
            {
                this.MatrixSize = 35;
            }
        }
    }
}
